import datetime
import os
import sys

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

#创建浏览器对象
class Driver:
    driver = ""
    def __init__(self):
        options = webdriver.ChromeOptions()
        #添加页面加载策略
        options.page_load_strategy = 'eager'
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
        # 添加隐式等待
        self.driver.implicitly_wait(2)

    def getScreenShot(self):
        #创建屏幕截图
        #图片文件名称：./2024-08-09-122332.png
        dirName = datetime.datetime.now().strftime("%Y-%m-%d")
        #判断dirName文件夹是否存在，如果不存在则创建文件夹
        if not os.path.exists("../images/"+dirName):
            os.mkdir("../images/"+dirName)

        #2024-0809-122332.png
        #图片文件路径:../images/调用方法-2024-08-09-122332.png
        #图片文件路径:../images/LoginFailTest-2024-08-10/2024-08-10-172321.png
        #图片文件路径:../images/LoginSucTest-2024-08-10/2024-08-10-172341.png
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
        self.driver.save_screenshot("../images/"+dirName+"/"+filename)

BlogDriver = Driver()